package com.googlecode.derby.customize.exception;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 例外処理.
 *
 * @author o.tanaka
 * @version 2009/12/11
 */
public class DbManagerException extends RuntimeException {

    /**
     * long serialVersionUID
     */
    private static final long serialVersionUID = -4171619149803323467L;

    /**
     * ロギングインスタンス.
     */
    private static final Log LOG =
        LogFactory.getLog(DbManagerException.class);

    private final String stackTrace;

    /**
     * @param message
     *            message
     * @param e
     *            Throwable
     */
    public DbManagerException(
            final String message, final Throwable e) {
        super(message);
        DbManagerException.LOG.error(message);
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        super.initCause(e);
        super.printStackTrace(pw);
        DbManagerException.LOG.error(sw.toString());
        this.stackTrace = sw.toString();

    }

    /**
     * @see java.lang.Throwable#printStackTrace()
     */
    @Override
    public final void printStackTrace() {
      this.printStackTrace(System.err);
    }
    /**
     * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
     * @param s PrintStream
     */
    @Override
    public final void printStackTrace(final java.io.PrintStream s) {
      synchronized (s) {
        s.print(this.stackTrace);
      }
    }
    /**
     * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
     * @param s PrintWriter
     */
    @Override
    public final void printStackTrace(final java.io.PrintWriter s) {
      synchronized (s) {
        s.print(this.stackTrace);
      }
    }

}
